﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using OrderSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



//ATTENTION:由于vs2022暂时无法基于Nunit生成测试文件，故暂时使用MSTestV2作为代替测试手段
namespace OrderSystem.TestsofService
{
    [TestClass()]
    public class OrderServiceTestsofService
    {
        private OrderService service;
        //private OrderService serviceAfterAdd = new OrderService();
        [TestMethod()]
        public void OrderServiceTestofService()
        {
            service = new OrderService();
            Assert.IsNotNull(service);
        }
        [TestMethod()]
        public void addOrderTestofService()
        {
            service = new OrderService();
            Assert.IsTrue(service.addOrder(new Order(new OrderDetails(new Good("toothpaste"), new Customer("intel"), 1499.0))));
            Assert.IsTrue(service.addOrder(new Order(new OrderDetails(new Good("Core i7"), new Customer("intel"), 1799.0))));
            Assert.IsTrue(service.addOrder(new Order(new OrderDetails(new Good("toothpaste"), new Customer("qualcomm"), 888.0))));
            Assert.IsFalse(service.addOrder(new Order(new OrderDetails(new Good("toothpaste"), new Customer("intel"), 1499.0))));
            Assert.IsTrue(service.addOrder(new Order(new OrderDetails(new Good("Ryzen5 5600x"), new Customer("amd"), 1599.0))));
            Assert.IsTrue(service.addOrder(new Order(new OrderDetails(new Good("8Gen1"), new Customer("qualcomm"), 999.0))));
            Assert.IsTrue(service.addOrder(new Order(new OrderDetails(new Good("new fresh air"), new Customer("nvidia"), 99999.0))));
            Assert.IsTrue(service.addOrder(new Order(new OrderDetails(new Good("A15 Bionic"), new Customer("Apple"), 4299.0))));
        }

        [TestMethod()]
        public void modifyOrderTestofService()
        {
            this.deleteOrderTestofService();
            Assert.IsTrue(service.modifyOrder(6, new OrderDetails(new Good("iPhone SE 3"), new Customer("Apple"), 4299.0)));
            Assert.IsFalse(service.modifyOrder(7, new OrderDetails(new Good("airTX 3060"), new Customer("nvidia"), 5099.0)));
            OrderService service1 = new OrderService();
            Assert.IsTrue(service1.addOrder(new Order(new OrderDetails(new Good("toothpaste"), new Customer("intel"), 1499.0))));
            Assert.IsTrue(service1.addOrder(new Order(new OrderDetails(new Good("Core i7"), new Customer("intel"), 1799.0))));
            Assert.IsTrue(service1.addOrder(new Order(new OrderDetails(new Good("toothpaste"), new Customer("qualcomm"), 888.0))));
            Assert.IsFalse(service1.addOrder(new Order(new OrderDetails(new Good("toothpaste"), new Customer("intel"), 1499.0))));
            Assert.IsTrue(service1.addOrder(new Order(new OrderDetails(new Good("Ryzen5 5600x"), new Customer("amd"), 1599.0))));
            Assert.IsTrue(service1.addOrder(new Order(5, new OrderDetails(new Good("new fresh air"), new Customer("nvidia"), 99999.0))));
            Assert.IsTrue(service1.addOrder(new Order(6, new OrderDetails(new Good("iPhone SE 3"), new Customer("Apple"), 4299.0))));
            for (int i = 0; i < 6; i++) { Assert.IsTrue(service.orderlist[i].Equals(service1.orderlist[i])); }


        }

        [TestMethod()]
        public void deleteOrderTestofService()

        {
            service = new OrderService();
            Assert.IsFalse(service.deleteOrder(new Order(new OrderDetails(new Good("8Gen1"), new Customer("qualcomm"), 999.0))));
            Assert.IsTrue(service.addOrder(new Order(new OrderDetails(new Good("toothpaste"), new Customer("intel"), 1499.0))));
            Assert.IsTrue(service.addOrder(new Order(new OrderDetails(new Good("Core i7"), new Customer("intel"), 1799.0))));
            Assert.IsTrue(service.addOrder(new Order(new OrderDetails(new Good("toothpaste"), new Customer("qualcomm"), 888.0))));
            Assert.IsFalse(service.addOrder(new Order(new OrderDetails(new Good("toothpaste"), new Customer("intel"), 1499.0))));
            Assert.IsTrue(service.addOrder(new Order(new OrderDetails(new Good("Ryzen5 5600x"), new Customer("amd"), 1599.0))));
            Assert.IsTrue(service.addOrder(new Order(new OrderDetails(new Good("8Gen1"), new Customer("qualcomm"), 999.0))));
            Assert.IsTrue(service.addOrder(new Order(new OrderDetails(new Good("new fresh air"), new Customer("nvidia"), 99999.0))));
            Assert.IsTrue(service.addOrder(new Order(new OrderDetails(new Good("A15 Bionic"), new Customer("Apple"), 4299.0))));
            Assert.IsTrue(service.deleteOrder(new Order(new OrderDetails(new Good("8Gen1"), new Customer("qualcomm"), 999.0))));
        }

        [TestMethod()]
        public void getOrderTestofService()
        {
            this.deleteOrderTestofService();
            Assert.AreEqual<Order>(service.getOrder(new Customer("Apple")).First(), new Order(new OrderDetails(new Good("A15 Bionic"), new Customer("Apple"), 4299.0)));
            Assert.IsTrue(service.getOrder(new Good("toothpaste")).Count() == 2);

        }

        [TestMethod()]
        public void sortTestofService()
        {
            List<Order> tmp = new List<Order>();
            tmp.Add(new Order(5, new OrderDetails(new Good("new fresh air"), new Customer("nvidia"), 99999.0)));
            tmp.Add(new Order(6, new OrderDetails(new Good("iPhone SE 3"), new Customer("Apple"), 4299.0)));
            tmp.Add(new Order(1, new OrderDetails(new Good("Core i7"), new Customer("intel"), 1799.0)));
            tmp.Add(new Order(3, new OrderDetails(new Good("Ryzen5 5600x"), new Customer("amd"), 1599.0)));
            tmp.Add(new Order(0, new OrderDetails(new Good("toothpaste"), new Customer("intel"), 1499.0)));
            tmp.Add(new Order(2, new OrderDetails(new Good("toothpaste"), new Customer("qualcomm"), 888.0)));
            this.modifyOrderTestofService();
            service.sort((left, right) =>
            {
                if (left.details.amount < right.details.amount) { return 1; }
                else if (left.details.amount == right.details.amount) { return 0; }
                else return -1;
            });
            for (int i = 0; i < 6; i++)
            {
                Assert.IsTrue(tmp[i].Equals(service.orderlist[i]));
            }



        }
        [TestMethod()]
        public void sortTestofService1()
        {
            this.modifyOrderTestofService();
            service.sort();
            OrderService service1 = new OrderService();
            Assert.IsTrue(service1.addOrder(new Order(0, new OrderDetails(new Good("toothpaste"), new Customer("intel"), 1499.0))));
            Assert.IsTrue(service1.addOrder(new Order(1, new OrderDetails(new Good("Core i7"), new Customer("intel"), 1799.0))));
            Assert.IsTrue(service1.addOrder(new Order(2, new OrderDetails(new Good("toothpaste"), new Customer("qualcomm"), 888.0))));
            Assert.IsTrue(service1.addOrder(new Order(3, new OrderDetails(new Good("Ryzen5 5600x"), new Customer("amd"), 1599.0))));
            Assert.IsTrue(service1.addOrder(new Order(5, new OrderDetails(new Good("new fresh air"), new Customer("nvidia"), 99999.0))));
            Assert.IsTrue(service1.addOrder(new Order(6, new OrderDetails(new Good("iPhone SE 3"), new Customer("Apple"), 4299.0))));
            for (int i = 0; i < 6; i++)
            {
                Assert.IsTrue(service1.orderlist[i].Equals(service.orderlist[i]));
            }

        }

        [TestMethod()]

        public void exportTestofService()
        {
            OrderService service1 = new OrderService();
            Assert.IsTrue(service1.addOrder(new Order(0, new OrderDetails(new Good("toothpaste"), new Customer("intel"), 1499.0))));
            Assert.IsTrue(service1.addOrder(new Order(1, new OrderDetails(new Good("Core i7"), new Customer("intel"), 1799.0))));
            Assert.IsTrue(service1.addOrder(new Order(2, new OrderDetails(new Good("toothpaste"), new Customer("qualcomm"), 888.0))));
            Assert.IsTrue(service1.addOrder(new Order(3, new OrderDetails(new Good("Ryzen5 5600x"), new Customer("amd"), 1599.0))));
            Assert.IsTrue(service1.addOrder(new Order(5, new OrderDetails(new Good("new fresh air"), new Customer("nvidia"), 99999.0))));
            Assert.IsTrue(service1.addOrder(new Order(6, new OrderDetails(new Good("iPhone SE 3"), new Customer("Apple"), 4299.0))));
            service1.export("OrderList.xml");
            Assert.IsTrue(System.IO.File.ReadAllText("./OrderList.xml") == System.IO.File.ReadAllText("../../OrderListTest.xml"));
        }

        [TestMethod()]
        public void importTestofService()
        {
            service = new OrderService();
            service.import("../../OrderListTest.xml");
            OrderService service1 = new OrderService();
            Assert.IsTrue(service1.addOrder(new Order(0, new OrderDetails(new Good("toothpaste"), new Customer("intel"), 1499.0))));
            Assert.IsTrue(service1.addOrder(new Order(1, new OrderDetails(new Good("Core i7"), new Customer("intel"), 1799.0))));
            Assert.IsTrue(service1.addOrder(new Order(2, new OrderDetails(new Good("toothpaste"), new Customer("qualcomm"), 888.0))));
            Assert.IsTrue(service1.addOrder(new Order(3, new OrderDetails(new Good("Ryzen5 5600x"), new Customer("amd"), 1599.0))));
            Assert.IsTrue(service1.addOrder(new Order(5, new OrderDetails(new Good("new fresh air"), new Customer("nvidia"), 99999.0))));
            Assert.IsTrue(service1.addOrder(new Order(6, new OrderDetails(new Good("iPhone SE 3"), new Customer("Apple"), 4299.0))));
            for (int i = 0; i < 6; i++)
            {
                Assert.IsTrue(service1.orderlist[i].Equals(service.orderlist[i]));
            }
        }
    }
}